package sporteam.web.server;

import java.io.IOException;
import java.util.ArrayList;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sporteam.common.PMF;
import sporteam.web.client.AEuser;
import sporteam.web.client.Activity;
import sporteam.web.client.GeoPtr;
import sporteam.web.client.Track;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;

@SuppressWarnings("serial")
public class addDebugInfo extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String access_token = req.getParameter("access_token");
		
		//Connect to facebook and retrive a user facebook (REST)
		FacebookClient facebookClient = new DefaultFacebookClient(
				access_token);	//establish connection with facebook-graph API with a specific access_token
		com.restfb.types.User facebook_user = facebookClient.fetchObject("me",
				com.restfb.types.User.class);	//me - the facebook user logged in ATM.

		//retrieve user from DB matching that fb_id
		PersistenceManager pm = PMF.get()
				.getPersistenceManager();
		AEuser user = pm.getObjectById(AEuser.class, facebook_user.getId());
		
		
		//set some made-up tracks to the user
		user.setUserTracks(madeupTracks());
		
		//add some made-up activities to user (shared to Bar and Tester)
		user.setActivity(madeUpActivityList());	
		
		//update user in DB
		pm.makePersistent(user);		// for some reason it's not willing to update the activities. puck it.
		
		//finishing
		pm.close();
		resp.getWriter().write("Done updating " + facebook_user.getName());
			
	}
	
	
	private ArrayList<Track> madeupTracks() {
		ArrayList<Track> trackArrayList = new ArrayList<Track>();
		for (int i = 1; i <= 20; i++) {
			Track track = new Track(0.5 * i, 100 * i, 10 * i, i, null);
			track.SetGeoPtrList(madeUpTrackList());
			trackArrayList.add(track);
		}
		return trackArrayList;
	}

	private ArrayList<GeoPtr> madeUpTrackList() {
		ArrayList<GeoPtr> returnList = new ArrayList<GeoPtr>();
		for (int i = 1; i <= 30; i++) {
			returnList.add(new GeoPtr(31.974 + i * 0.001
					* (Math.random() * 2 - 1), 34.813 - i * 0.001
					* (Math.random() * 2 - 1), 0, 0, 0));
		}
		return returnList;
	}

	private Activity madeUpActivity() {
		Track senderTrack = new Track(10, 10, 10, 10, madeUpTrackList());
		senderTrack.setName("sender");
		Track receiverTrack = new Track(11, 11, 11, 11, madeUpTrackList());
		receiverTrack.setName("receiver");
		Activity activity = new Activity(senderTrack, "1507604362",
				"100003730751502", "shmeko", "beko");
		activity.setReceiverTrack(receiverTrack);

		return activity;
	}
	private ArrayList<Activity> madeUpActivityList(){
		ArrayList<Activity> returnList = new ArrayList<Activity>();
		returnList.add(madeUpActivity());
		return returnList;
	}
	
}


